D:\a\tools.proto\tools.proto\dynamic\src\buffer\builder.rs
Line | Count | Source |
1 | | // Copyright (c) 2025, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use std::cell::{Cell, UnsafeCell}; |
30 | | use std::rc::Rc; |
31 | | use crate::buffer::buffer::Buffer; |
32 | | use crate::buffer::unsafe_buffer::UnsafeBuffer; |
33 | | use crate::buffer::BufferView; |
34 | | use crate::buffer::view::{Location, PathComponent}; |
35 | | use crate::component::Component; |
36 | | use crate::field::primitive::PrimitiveType; |
37 | | |
38 | | pub struct Builder<'a> { |
39 | | name: String, |
40 | | children: Vec<BufferView<'a>>, |
41 | | location: Location, |
42 | | component: Option<Box<dyn Component>>, |
43 | | flat: Rc<Cell<bool>>, |
44 | | primitive: Option<Box<dyn PrimitiveType>> |
45 | | } |
46 | | |
47 | | impl<'a> Builder<'a> { |
48 | 39 | pub fn new<S: Into<String>>(name: S) -> Builder<'a> { |
49 | 39 | Self { |
50 | 39 | name: name.into(), |
51 | 39 | children: Vec::new(), |
52 | 39 | location: Location { |
53 | 39 | fixed: false, |
54 | 39 | offset: -1, |
55 | 39 | size: 0, |
56 | 39 | }, |
57 | 39 | component: None, |
58 | 39 | flat: Rc::new(Cell::new(false)), |
59 | 39 | primitive: None |
60 | 39 | } |
61 | 39 | } |
62 | | |
63 | 16 | pub fn primitive(mut self, primitive: Box<dyn PrimitiveType>) -> Self { |
64 | 16 | self.primitive = Some(primitive); |
65 | 16 | self |
66 | 16 | } |
67 | | |
68 | 16 | pub fn fixed(mut self, offset: usize, size: usize) -> Builder<'a> { |
69 | 16 | self.location.fixed = true; |
70 | 16 | self.location.offset = offset as _; |
71 | 16 | self.location.size = size; |
72 | 16 | self |
73 | 16 | } |
74 | | |
75 | 6 | pub fn size(mut self, size: usize) -> Builder<'a> { |
76 | 6 | self.location.size = size; |
77 | 6 | self |
78 | 6 | } |
79 | | |
80 | 6 | pub fn component(mut self, component: impl Component + 'static) -> Builder<'a> { |
81 | 6 | self.component = Some(Box::new(component)); |
82 | 6 | self |
83 | 6 | } |
84 | | |
85 | 25 | pub fn add_child(mut self, mut builder: Builder<'a>) -> Builder<'a> { |
86 | 25 | builder.flat = self.flat.clone(); |
87 | 25 | self.children.push(builder.get()); |
88 | 25 | self |
89 | 25 | } |
90 | | |
91 | 33 | fn get(self) -> BufferView<'a> { |
92 | 33 | BufferView { |
93 | 33 | path_component: Rc::new(PathComponent { |
94 | 33 | name: self.name, |
95 | 33 | index: Cell::new(-1), |
96 | 33 | parent: UnsafeCell::new(None), |
97 | 33 | }), |
98 | 33 | buffer: Buffer { |
99 | 33 | unsafe_buffer: UnsafeBuffer::Borrowed(b""), |
100 | 33 | flat: self.flat, |
101 | 33 | offset: 0 |
102 | 33 | }, |
103 | 33 | children: self.children, |
104 | 33 | location: self.location, |
105 | 33 | component: self.component, |
106 | 33 | primitive: self.primitive, |
107 | 33 | items: None |
108 | 33 | } |
109 | 33 | } |
110 | | |
111 | 8 | pub fn build(self, init_mem: bool) -> BufferView<'a> { |
112 | 8 | let motherfuckingrust = self.location.size; |
113 | 8 | let mut view = self.get(); |
114 | 8 | setup_parents(&mut view); |
115 | 8 | if motherfuckingrust > 0 && init_mem3 { Branch (115:12): [True: 0, False: 1]
Branch (115:37): [True: 0, False: 0]
Branch (115:12): [True: 3, False: 4]
Branch (115:37): [True: 1, False: 2]
|
116 | 1 | view.buffer.unsafe_buffer = UnsafeBuffer::with_capacity(motherfuckingrust); |
117 | 7 | } |
118 | 8 | view |
119 | 8 | } |
120 | | } |
121 | | |
122 | 33 | fn setup_parents(view: &mut BufferView) { |
123 | 58 | for child25 in &mut view.children { |
124 | 25 | *unsafe { &mut *child.path_component.parent.get() } = Some(view.path_component.clone()); |
125 | 25 | setup_parents(child); |
126 | 25 | } |
127 | 33 | } |